package cn.edu.cxcyzx.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.edu.cxcyzx.algorithm.CalMd5;
import cn.edu.cxcyzx.entity.StuAndGroup;
import cn.edu.cxcyzx.entity.Student;
import cn.edu.cxcyzx.entity.User;
import cn.edu.cxcyzx.service.StudentService;
import cn.edu.cxcyzx.supervisor.DaoFit;
import cn.edu.cxcyzx.supervisor.ResultDo;
import cn.edu.cxcyzx.util.PropertyPlaceholder;

@Service("StudentService")
public class StudentServiceImpl implements StudentService {
	
	@Resource
	public DaoFit daoFit;

	public ResultDo<List<Student>> getAllStudent(String stuName) {
		// TODO Auto-generated method stub
		ResultDo<List<Student>> resultDo=new ResultDo<List<Student>>();
		List<Student> list=null;
		if(stuName==null||stuName.equals("")){
			list=daoFit.getStudentDao().getAllStudent();
		}else{
			System.out.println("service--查找："+stuName);
			list=daoFit.getStudentDao().getAllStudent_stuName(stuName);
		}
		if(list==null||list.size()==0){
			resultDo.setSuccess(false);
		}else{
			resultDo.setSuccess(true);
			resultDo.setResult(list);
		}
		return resultDo;
	}


	@Override
	public ResultDo<String> insertStu(Student stu) {
		// TODO Auto-generated method stub
		ResultDo<String> resultDo=new ResultDo<>(); 
		if(stu!=null){
			Student stu_=daoFit.getStudentDao().getStudent(stu.getStuId());
			if(stu_!=null){
				resultDo.setSuccess(false);
				resultDo.setMessage("该学号的学生信息已存在系统中");
				return resultDo;
			}
			int count1=daoFit.getStudentDao().insertStu(stu);
			String secret=CalMd5.MD5((String)PropertyPlaceholder.getProperty("DEFAULT_PASSWORD"),stu.getStuId());
			User u=new User(stu.getStuId(),secret);
			u.setRole(2);
			int count2=daoFit.getUserDao().insertUser(u);
			System.out.println("count1:"+count1+",count2:"+count2);
			if(count1>0&&count2>0){
				resultDo.setSuccess(true);
				resultDo.setMessage("学生添加成功");
			}
		}
		return resultDo;
	}

	@Override
	public void updateStu(Student stu) {
		// TODO Auto-generated method stub
		if(stu!=null){
			daoFit.getStudentDao().upstuInfo(stu);
		}
	}

	@Override
	public ResultDo<Student> getStu_byStuId(String stuId) {
		// TODO Auto-generated method stub
		ResultDo<Student> resultDo=new ResultDo<Student>();
		Student stu=daoFit.getStudentDao().getStudent(stuId);
		if(stu==null){
			resultDo.setSuccess(false);
		}else{
			resultDo.setResult(stu);
			resultDo.setSuccess(true);
		}
		return resultDo;
	}

	@Override
	public String getStuName(String stuId) {
		// TODO Auto-generated method stub
		return daoFit.getStudentDao().getStuName(stuId);
	}

}
